<?php

//用户控制器
class UserController extends Controller
{	
	private $model = null;//存放model对象

	//构造方法自动new出model模型
	public function __construct()
	{
		//调用执行父级的构造方法
		parent::__construct();
		$this->model = new Model('usejs');
	}


	public function index()
	{	
		$data = $this->model->order('id desc')->select();
		$this->assign('title','用户列表');
		$this->assign('list', $data);
		$this->display('User/index.html');
	}

	//删除
	public function del()
	{
		if($this->model->del($_GET['id'])){
			$this->redirect('恭喜你,删除成功!','./index.php?c=User');
		} else {
			$this->redirect('删除失败,请重试!','./index.php?c=User');
		}
	}

	//加载添加页面
	public function add()
	{
		$this->assign('title','添加用户');
		$this->display('User/add.html');
	}

	//执行添加操作
	public function insert()
	{
		if ($this->model->add() > 0) {
			$this->redirect('恭喜您,添加成功!','./index.php?c=User');
		} else {
			$this->redirect('添加失败,请重试!');
		}
	}

	//加载编辑页面
	public function edit()
	{
		$data = $this->model->find($_GET['id']);
		$this->assign('title', '编辑用户');
		$this->assign('data',$data);
		$this->display('User/edit.html');
	}

	//执行编辑操作
	public function update()
	{
		if ($this->model->update()) {
			$this->redirect('恭喜您,编辑成功!','./index.php?c=User');
		} else {
			$this->redirect('编辑失败,请重试!');
		}
	}
}